package com.shengdong.water.code.utils

import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import androidx.core.graphics.toColorInt

class DecorationUtil (
    private val dividerHeight: Int = DEFAULT_DIVIDER_HEIGHT,
    private val dividerColor: Int = DEFAULT_DIVIDER_COLOR,
    private val orientation: Int = RecyclerView.VERTICAL
    ) : RecyclerView.ItemDecoration() {

    companion object {
        private const val DEFAULT_DIVIDER_HEIGHT = 1 // 默认1dp的分割线
        private val DEFAULT_DIVIDER_COLOR = "#DDDDDD".toColorInt() // 默认浅灰色
    }

        private val paint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
            color = dividerColor
            style = Paint.Style.FILL
        }

        override fun getItemOffsets(
            outRect: Rect,
            view: View,
            parent: RecyclerView,
            state: RecyclerView.State
        ) {
            val position = parent.getChildAdapterPosition(view)
            val itemCount = parent.adapter?.itemCount ?: 0

            // 最后一个item不设置间距
            if (position == itemCount - 1) return

            if (orientation == RecyclerView.VERTICAL) {
                outRect.bottom = dividerHeight
            } else {
                outRect.right = dividerHeight
            }
        }

        override fun onDraw(
            canvas: Canvas,
            parent: RecyclerView,
            state: RecyclerView.State
        ) {
            if (orientation == RecyclerView.VERTICAL) {
                drawVertical(canvas, parent)
            } else {
                drawHorizontal(canvas, parent)
            }
        }

        private fun drawVertical(canvas: Canvas, parent: RecyclerView) {
            val left = parent.paddingLeft
            val right = parent.width - parent.paddingRight

            for (i in 0 until parent.childCount - 1) {
                val child = parent.getChildAt(i)
                val position = parent.getChildAdapterPosition(child)
                val itemCount = parent.adapter?.itemCount ?: 0

                if (position == itemCount - 1) continue

                val params = child.layoutParams as RecyclerView.LayoutParams
                val top = child.bottom + params.bottomMargin
                val bottom = top + dividerHeight

                canvas.drawRect(
                    left.toFloat(),
                    top.toFloat(),
                    right.toFloat(),
                    bottom.toFloat(),
                    paint
                )
            }
        }

        private fun drawHorizontal(canvas: Canvas, parent: RecyclerView) {
            val top = parent.paddingTop
            val bottom = parent.height - parent.paddingBottom

            for (i in 0 until parent.childCount - 1) {
                val child = parent.getChildAt(i)
                val position = parent.getChildAdapterPosition(child)
                val itemCount = parent.adapter?.itemCount ?: 0

                if (position == itemCount - 1) continue

                val params = child.layoutParams as RecyclerView.LayoutParams
                val left = child.right + params.rightMargin
                val right = left + dividerHeight

                canvas.drawRect(
                    left.toFloat(),
                    top.toFloat(),
                    right.toFloat(),
                    bottom.toFloat(),
                    paint
                )
            }
        }
}